package com.map.recreation.co.coloradorecreationmap;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterManager;
import com.map.recreation.co.provider.CampSite;
import com.map.recreation.co.provider.RecreationDB;

import java.util.List;

public class MapsActivity extends Activity {

    private static final String TAG = "MapsActivity";

    private static final LatLng SUMMIT_COUNTY_COLORADO = new LatLng(39.607827, -106.058563);
    private GoogleMap map;
    private RecreationDB recreationDB;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private ClusterManager<CampSite> clusterManager;

    public MapsActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        recreationDB = new RecreationDB(getApplicationContext());

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        clusterManager = new ClusterManager<CampSite>(this, map);
        clusterManager.setRenderer(new MyClusterRenderer(this, map, clusterManager));
        clusterManager.setOnClusterInfoWindowClickListener(new ClusterManager.OnClusterInfoWindowClickListener<CampSite>() {
            @Override
            public void onClusterInfoWindowClick(Cluster<CampSite> campSiteCluster) {
                Log.d(TAG, "onClusterInfoWindowClick() method...");
            }
        });
        clusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<CampSite>() {
            @Override
            public void onClusterItemInfoWindowClick(CampSite site) {
                Log.d(TAG, "onClusterItemInfoWindowClick() method...");
            }
        });
        clusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<CampSite>() {
            @Override
            public boolean onClusterClick(Cluster<CampSite> campSiteCluster) {
                Log.d(TAG, "onClusterCLick() method...");
                return false;
            }
        });

        map.setOnCameraChangeListener(clusterManager);
        map.setOnMarkerClickListener(clusterManager);
        map.setOnInfoWindowClickListener(clusterManager);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                Log.d(TAG, "new location: " + location);
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };

        // Register the listener with the Location Manager to receive location updates
        if(locationManager != null) {
            Log.d(TAG, "Requesting location updates.");
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

//            locationListener.setLastKnownNetworkLocation(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
//            locationListener.setLastKnownGpsLocation(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (map!=null) {

            List<CampSite> sites = recreationDB.getAllCampSites();
            Log.d(TAG, "sites size: " + sites.size());

            clusterManager.addItems(sites);

            //Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(SUMMIT_COUNTY_COLORADO, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            } else {
            Log.d(TAG, "map is null");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        locationManager.removeUpdates(locationListener);
        locationListener = null;
        locationManager = null;
    }
}